// pages/user/user.js
var app = getApp()
var httpService = app.data.service;
Page({
  data: {
    order:[],
    haveData:true,
  },
  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'userid',
      success: function (res) {
        wx.request({
          url: httpService + '/index.php/getorderbyuserid',
          data: {
            userid: res.data
          },
          success: function (res) {
            var data = res.data;
            if (data.length > 0) {
              that.setData({
                haveData: false,
                order: res.data
              })
            }
          }
        })
      },
    })
  },
  onShow:function(){
    
  },

  /**
   * 点击评价 跳转到评价页面
   */
  pjClick:function(e){
    //获得订单编号
    var orderId = e.currentTarget.id;
    wx.navigateTo({
      url: 'pj/pj?orderId='+orderId,
    })
  },
  /**
   * 查看已经完成订单
   */
  accomplish:function(e){
    var that = this;
    wx.getStorage({
      key: 'userid',
      success: function (res) {
        wx.request({
          url: httpService + '/index.php/getorderbyuserid2',
          data: {
            userid: res.data
          },
          success: function (res) {
            var data = res.data;
            if (data.length > 0) {
              that.setData({
                haveData: false,
                order: res.data
              })
            } else {
              that.setData({
                haveData: true,
                order: res.data
              })
            }
          }
        })
      },
    })
  },
  /**
   * 点击查看未完成订单
   */
  unaccomplish:function(){
    var that = this;
    wx.getStorage({
      key: 'userid',
      success: function (res) {
        wx.request({
          url: httpService + '/index.php/getorderbyuserid',
          data: {
            userid: res.data
          },
          success: function (res) {
            var data = res.data;
            if (data.length > 0) {
              that.setData({
                haveData: false,
                order: res.data
              })
            }else{
              that.setData({
                haveData: true,
                order: res.data
              })
            }
          }
        })
      },
    })
  },
  /**
   * 点击申请退款
   */
  applay:function(e){
    var orderId = e.currentTarget.id;
    var that = this;
    wx.request({
      url: httpService + '/index.php/refund',
      data:{
        orderId:orderId
      },
      success:function(res){
        
        wx.getStorage({
          key: 'userid',
          success: function (res) {
            wx.request({
              url: httpService + '/index.php/getorderbyuserid',
              data: {
                userid: res.data
              },
              success: function (res) {
                var data = res.data;
                if (data.length > 0) {
                  that.setData({
                    haveData: false,
                    order: res.data
                  })
                } else {
                  that.setData({
                    haveData: true,
                    order: res.data
                  })
                }
              }
            })
          },
        })
      }
    })
  }

})